package com.bayi.program;

import android.app.Application;

import com.android.tony.defenselib.DefenseCrash;
import com.android.tony.defenselib.handler.IExceptionHandler;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author Administrator on 2025-05-14.
 * <p>
 * 站在顶峰 看世界
 * 落到谷底 思人生
 */
public class BaseApplication extends Application implements IExceptionHandler {

    private static BaseApplication app;

    public static BaseApplication getApp() {
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;

        DefenseCrash.initialize(this);
        DefenseCrash.install(this);
    }


    private static final int MAX_STACK_TRACE_SIZE = 131071; //128 KB - 1
    private static final int MAX_ACTIVITIES_IN_LOG = 50;

    @Override
    public void onCaughtException(Thread thread, Throwable throwable, boolean isSafeMode, boolean isCrashInChoreographer) throws Throwable {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        throwable.printStackTrace(pw);
        String stackTraceString = sw.toString();

        if (stackTraceString.length() > MAX_STACK_TRACE_SIZE) {
            String disclaimer = " [stack trace too large]";
            stackTraceString = DateUtils.getStrTime() + stackTraceString.substring(0, MAX_STACK_TRACE_SIZE - disclaimer.length()) + disclaimer;
        }

//        MyLogcat.writeCrashToFile(stackTraceString);

        throwable.printStackTrace();
    }
}
